/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package midpmpc.ui;

import java.util.Timer;
import java.util.TimerTask;
import javax.microedition.lcdui.Graphics;
//import midpmpc.CallbackMenuItem.MenuListener;
//import midpmpc.CallbackMenuItem;
import org.j4me.logging.Log;
import org.j4me.ui.MenuItem;
import org.j4me.ui.Theme;
import org.j4me.ui.components.Component;

/**
 *
 * @author vvs
 */
public class MenuOption extends Component{

    String _text="null";
    MenuListener _parent;
    Timer scrollTimer;
    boolean _selectable=true;
    boolean doScroll=true;
    int scrollPos=0;
    class ScrollAction extends TimerTask
    {
        int ticks=0;
        MenuOption _opt;
        public ScrollAction(MenuOption opt)
        {
            _opt=opt;
        }
        public void run()
        {
            _opt.scroll();
        }
    }
    public interface MenuListener
    {
        public void onMenuSelection (MenuOption item);
    }
    public MenuOption(String text,MenuListener listener)
    {
        _parent=listener;
        setString(text);
    }
    public MenuOption(String text)
    {
        _text=text;
    }
    public MenuOption(String text,boolean selectable)
    {
        _selectable=selectable;
    }
    protected int[] getPreferredComponentSize (Theme theme, int viewportWidth, int viewportHeight)
    {
        int height=theme.getFont().getHeight();
        return new int[] {viewportWidth,height};
    }
    protected void paintComponent (Graphics g, Theme theme, int width, int height, boolean selected)
    {
        int backgroundColor = selected ?
                        theme.getHighlightColor() :
                        theme.getBackgroundColor();
        g.setColor( backgroundColor );
        g.fillRect( 0, 0, width, height );
        int fontColor = selected ?
                theme.getBackgroundColor() :
                theme.getFontColor();
        g.setFont(theme.getFont());
        g.setColor(fontColor);
        if (theme.getFont().stringWidth(_text)>width)
            doScroll=true;
        else
            doScroll=false;
        if ((scrollTimer!=null) && (!selected))
        {
            scrollTimer.cancel();
            scrollTimer=null;
            scrollPos=0;
        }
        if ((scrollTimer==null) && selected && doScroll)
        {
            scrollTimer=new Timer();
            scrollTimer.scheduleAtFixedRate(new ScrollAction(this), 2000, 100);
        }

        g.drawString(_text.substring(scrollPos), 0,0, Graphics.LEFT|Graphics.TOP);

    }
    public boolean acceptsInput()
    {
        return _selectable;
    }
    public void keyPressed (int key)
    {
        Log.warn("menuoption: key "+String.valueOf(key));
    }
    public void onSelection()
    {
        if (_parent!=null)
            _parent.onMenuSelection(this);
    }
    public String getText()
    {
        return _text;
    }
    void scroll()
    {
        scrollPos=(scrollPos+1)%_text.length();
        repaint();
    }
    public void setString(Object obj)
    {
        if (obj!=null)
            setString(obj.toString());
        else
            setString("null");
    }
    public void setString(String str)
    {
        _text=str;
        repaint();
    }

}
